package com.example.myapplication.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;

/**
 * @Author: Ng
 * @Date: 2024/4/7
 * @Versopn: 1.0.0
 */
public class SplashActivity extends AppCompatActivity {

    private LinearLayout linearLayout;

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash);
        linearLayout = findViewById(R.id.splash_linearLayout);
        // 设置渐变效果
        setAlphaAnimation();
    }

    /**
     * 设置渐变效果
     */
    private void setAlphaAnimation() {
        // 生成动画对象
        AlphaAnimation animation = new AlphaAnimation(0.5f, 1.0f);
        // 设置持续时间 3s
        animation.setDuration(2000);
        // 给空间设置动画
        linearLayout.setAnimation(animation);
        // 设置动画监听
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                jumpActivity();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 根据首次启动应用与否跳转到相应界面
     */
    private void jumpActivity() {
        SharedPreferences sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
        String first = sharedPreferences.getString("isFirst", "0");
        Intent intent = new Intent();
        if (first.equals("0")) {
            intent.setClass(this, GuideActivity.class);
        } else {
            intent.setClass(this, MainActivity.class);
        }
        startActivity(intent);
        finish();
    }
}
